package com.liruo.test.spring.annotationdep.bean.condidtion;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @Author:liruo
 * @Date:2022-11-20-19:50:48
 * @Desc
 */
public class Win32Condition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        System.out.println("==========================Win32Condition");
        MergedAnnotations annotations = metadata.getAnnotations();
        annotations.forEach(annotationMergedAnnotation ->{
            System.out.println(new Date());
            Map<String, Object> stringObjectMap = annotationMergedAnnotation.asMap(MergedAnnotation.Adapt.ANNOTATION_TO_MAP, MergedAnnotation.Adapt.CLASS_TO_STRING);
            System.out.println(stringObjectMap);
        });

        Arrays.stream(context.getRegistry().getBeanDefinitionNames()).forEach(System.out::println);

        Environment environment = context.getEnvironment();
        String vmMyEv1 = environment.getProperty("vm.my.ev1");
        System.out.println(vmMyEv1);
        String vmMYEv2 = environment.getProperty("vm.my.ev2");
        System.out.println(vmMYEv2);
        String sysMyEv1 = environment.getProperty("sys.my.ev1");
        System.out.println(sysMyEv1);
        String sysMyEv2 = environment.getProperty("sys.my.ev2");
        System.out.println(sysMyEv2);
        System.out.println("==========================");
        String os = environment.getProperty("os.name");
        if(os != null && os.contains("win32")){
            System.out.println("has win32 environment");
            return true;
        }
        return false;
    }
}
